package com.mz.ozy.mvp.presenter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.BookDetailBean;
import com.mz.ozy.bean.FamilyFeastBean;
import com.mz.ozy.bean.OnlineFamilyBean;
import com.mz.ozy.bean.OnlineListBean;
import com.mz.ozy.bean.OrderBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.BookDetailModel;
import com.mz.ozy.mvp.model.PrivateCustomModel;
import com.mz.ozy.mvp.view.BookDetailContract;
import com.mz.ozy.mvp.view.PrivateCustomContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.net.RetrofitUtils;
import com.mz.ozy.utils.JsonUtils;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.RxUtil;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;

/**
 * @author lifeStyle on 2020/4/26 0026.
 */
public class BookDetailPresenter extends BasePresenterImpl<BookDetailContract.View> implements BookDetailContract.Presenter {
    private BookDetailModel shogModel;

    public BookDetailPresenter() {
        shogModel = new BookDetailModel();
    }

    @Override
    public void queryDetail(Context context, String id) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", id);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10096, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<BookDetailBean>(mView, true) {

                    @Override
                    public void onSuccesss(BookDetailBean orderBean) {
                        mView.queryDetailSuccess(orderBean);
                    }
                });
    }


    @Override
    public void onPay(Context contex, String orderId, String type, String pwd) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", orderId);
        maps.put("payType", type);
        maps.put("payPwd", MD5Util.getMD5String(pwd));
        RetrofitUtils.getApiService(contex).post(Constants.TransCode.TransCode10094, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<JSONObject>(mView, true) {

                    @Override
                    public void onSuccesss(JSONObject orderBean) {
                        mView.onPaySuccess(orderBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        mView.onPayError("");
                    }
                }.setTransCode(Constants.TransCode.TransCode10094));
    }

    public void cancelBook(Context context, String id) {
        Map<String, String> maps = new HashMap<>();
        maps.put("id", id);
        RetrofitUtils.getApiService(context).post(Constants.TransCode.TransCode10097, JsonUtils.requestJsonDate(maps), Constants.tokenId)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<OnlineListBean>(mView, true) {

                    @Override
                    public void onSuccesss(OnlineListBean orderBean) {
                        mView.cancelBookSuccess(orderBean);
                    }
                });
    }

}
